home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
FREC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-25
|
7KB
|
242 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: app.cpp
// Title: Application Template
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
// This module contains the program entry point for
//
// The code in this module may be written in C++ or C.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
//
//----------------------------------------------------------------------------
#include <base.h>
//----------------------------------------------------------------------------
// Stack size
//----------------------------------------------------------------------------
#if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
unsigned _stklen = 0x4000;
#endif
//----------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------
//
// A useless string which is simply embedded in the executable.
//
PSZ __pszCredits__ = "Written by John M Weeder, 1993";
//
// This should contain a program description which will be displayed as
// part of the program's help text.
//
static PCSZ pcszDescription =
"This program extracts records from a fixed length record data file.";
//
// Program command line options
//
#if OS_UNIX
#define MAX_BUFFER ((SIZET)(1024 * 1024 * 4))
#else
#define MAX_BUFFER ((SIZET)0xF000)
#endif
static LONG lRecSize = 0;
static LONG lStart = 1;
static LONG lEnd = -1;
static LONG lLength = -1;
static LONG lHeader = 0;
static LONG lSkip = 0;
static BOOL fAppend = FALSE;
static CHAR szInput[MAX_PATH];
static CHAR szOutput[MAX_PATH];
static HF hfInput = -1;
static HF hfOutput = -1;
static PBYTE pb = NULL;
static BS_CMDOPT acmdopt[] =
{
{ "APPEND", (PVOID)&fAppend, CMDOPT_TRUE, "Append to output file?"},
{ "BEGIN", (PVOID)&lStart, CMDOPT_NUMERIC, "First record to process."},
{ "END", (PVOID)&lEnd, CMDOPT_NUMERIC, "Last record to process."},
{ "HEADER", (PVOID)&lHeader, CMDOPT_NUMERIC, "Size of header in bytes."},
{ "LENGTH", (PVOID)&lLength, CMDOPT_NUMERIC, "Number of records to process."},
{ "RECSIZE", (PVOID)&lRecSize, CMDOPT_NUMERIC, "Size of record in bytes."},
{ "SKIP", (PVOID)&lSkip, CMDOPT_NUMERIC, "Number of records to skip."},
{ "input", (PVOID)szInput, CMDOPT_FILESPECR(80), "Input file name."},
{ "output", (PVOID)szOutput, CMDOPT_FILESPECR(80), "Output file name."},
BS_CMDOPT_NULL,
};
//----------------------------------------------------------------------------
// Description:
// Parameters:
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
static SHORT FN_L Process(void)
{
FLAG16 fsInput = FL_OPEN|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
FLAG16 fsOutput = FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
PCSZ pcszData = EnvGet("DATA");
FPOS fsize;
FPOS fpos;
LONG lRecords;
SIZET cBuf;
if (lRecSize <= 0) // Check record size
{
Output("Invalid records size specified.\n");
return 95;
}
if (lRecSize > 8192) // Set default parameter values
lRecSize = 8192;
if (lStart < 1)
lStart = 1;
if (lSkip < 0)
lSkip = 0;
// Open input file
if (!FnameQualify(szInput, NULL, pcszData, 0))
return 99;
if (!FileOpen(&hfInput, szInput, fsInput, NULL))
return 98;
Output("Opened input file '%s'\n", szInput);
// Open output file
if (!FnameQualify(szOutput, NULL, pcszData, 0))
return 97;
if (!fAppend)
fsOutput |= (FL_CREATE|FL_TRUNCATE);
else
fsOutput |= (FL_CREATE|FL_OPEN);;
if (!FileOpen(&hfOutput, szOutput, fsOutput, NULL))
return 96;
Output("Opened output file '%s'\n", szOutput);
fsize = FileGetSize(hfInput); // Get input file size
if (fsize < 0)
return 91;
fsize = MAX(fsize - lHeader, 0);
if (lRecSize > fsize)
lRecSize = fsize;
fpos = FileGetSize(hfOutput); // Get output file size
if (fpos < 0)
return 90;
lRecords = fsize / lRecSize;
if ((fsize % lRecSize) > 1) // Verify integral number of records
{ // Extra byte allowed is for Ctrl-Z
Output("%ld extra bytes ignored.\n", (fsize % lRecSize));
}
if (lRecords == 0)
{
Output("No records found in input file.\n");
return 0;
}
if (lStart > lRecords)
{
Output("No records copied.\n");
return 0;
}
if (lLength > 0)
lEnd = lStart + lLength - 1;
if (lEnd > lRecords || lEnd < lStart)
lEnd = lRecords;
if (lSkip)
cBuf = 1;
else
cBuf = MAX_BUFFER / (SIZET)lRecSize;
pb = MemAlloc(cBuf * (SIZET)lRecSize);
if (pb == NULL)
{
Output("Unable to allocate copy buffer.\n");
return 94;
}
Output("Record size is %ld bytes.\n", lRecSize);
Output("Copying records %ld to %ld", lStart, lEnd);
if (lSkip)
Output(", every %ld%s record", lSkip+1, strord(lSkip+1));
Output(".\n");
while (lStart <= lEnd)
{
SIZET cCopy = (SIZET)MIN((LONG)cBuf, (lEnd - lStart + 1));
SIZET cCopyBytes = cCopy * (SIZET)lRecSize;
Output("\r%08ld", lStart);
if (!FileRead(hfInput, pb, cCopyBytes, ((lStart - 1) * lRecSize + lHeader)))
return 93;
if (!FileWrite(hfOutput, pb, cCopyBytes, fpos))
return 92;
fpos += (FPOS)cCopyBytes;
lStart += (LONG)cCopy;
lStart += lSkip;
}
Output("\r%08ld\n", lEnd);
return 0;
}
//----------------------------------------------------------------------------
// Description: main() - Program entry point
// Parameters: Standard C parameters
// Returns: DOS return code.
//----------------------------------------------------------------------------
int main(int argc, char **argv)
{
static BS_CFG cfg = CFG_DFT;
SHORT sResult;
//
// Initialize base library
//
BaseLibraryInitialize(argc, argv, &cfg);
BaseTitle("$Revision: 93.1 $", __DATE__, __TIME__, "Fixed Length Record File Utility");
if (!BaseTitleHelp(acmdopt, pcszDescription, 0))
return 99;
sResult = Process();
Output(sResult ? "Failed.\n": "Success.\n");
if (hfInput >= 0)
FileClose(hfInput);
if (hfOutput >= 0)
FileClose(hfOutput);
if (pb)
MemFree(pb);
return sResult;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------